<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>智慧社区后台</title>
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
<link rel="stylesheet" href="${pageContext.request.contextPath}/plugins/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/AdminLTE.min.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/plugins/bootstrap-table/css/bootstrap-table.css">
<link rel="stylesheet" href="${pageContext.request.contextPath}/plugins/bootstrap-switch/css/bootstrap-switch.min.css" />
</head>
<style>
</style>
<body style="padding-bottom: 90px; background-color: #ECF0F5;">
	<section class="content" style="margin: 15px; background-color: white;">
		<div class="row">
			<div class="col-sm-12">
				<table id="table">
					<thead style="background-color: #00C0EF">
						<tr style="background-color: #00C0EF">
							<th class="text-center" data-field="communityName">房屋信息</th>
							<th class="text-center" data-field="name">姓名</th>
							<th class="text-center" data-field="gender">性别</th>
							<th class="text-center" data-field="idCard">身份证号</th>
							<th class="text-center" data-field="tel">联系电话</th>
							<th class="text-center" data-field="emergencyContact">紧急联系人</th>
							<th class="text-center" data-field="emergencyContactTel">紧急联系人电话</th>
							<th class="text-center" data-field="type">状态</th>
							<th class="text-center" data-field="id">操作</th>
						</tr>
					</thead>
				</table>
			</div>
		</div>
	</section>
</body>
<script src="${pageContext.request.contextPath}/plugins/jQuery/jquery-2.2.3.min.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap/js/bootstrap.min.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table-zh-CN.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table-export.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/tableExport.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table-toolbar.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table-filter-control.js"></script>
<script src="${pageContext.request.contextPath}/js/ajaxfileupload.js"></script>
<script src="${pageContext.request.contextPath}/js/date.js"></script>
<!-- layer -->
<script src="${pageContext.request.contextPath}/plugins/layer/2.4/layer.js"></script>
<script>

	function actionFormatter(value, row, index) {
		//console.log(row);
		if(row.type==0){
			return [ "<button class='btn btn-info btn-xs glyphicon glyphicon-file' onclick='addOwner(\"" + row.id + "\")'>编辑</button>",
				"&nbsp;<button class='btn btn-danger btn-xs glyphicon glyphicon-trash' onclick='delOwner(\"" + row.id + "\")'>迁入</button>",].join('');
		}else{
			return [ "<button class='btn btn-info btn-xs glyphicon glyphicon-file' onclick='addOwner(\"" + row.id + "\")'>编辑</button>",
				"&nbsp;<button class='btn btn-danger btn-xs glyphicon glyphicon-trash' onclick='delOwner(\"" + row.id + "\")'>迁出</button>",].join('');
		}
	}
	
	var table_dataset = [ {
		field : 'communityName',
		align : 'center',
		formatter : function Formatter(value, row, index) {
			return row.communityName+"-"+row.numberPeriods+"-"+row.building+"-"+row.unit+"-"+row.doorCard
		}
	}, {
		field : 'name',
		align : 'center',
	}, {
		field : 'gender',
		align : 'center',
	}, {
		field : 'idCard',
		align : 'center',
		/* formatter : function Formatter(value, row, index) {
			return row.unit+"单元"
		} */
	}, {
		field : 'tel',
		align : 'center',
	}, {
		field : 'emergencyContact',
		align : 'center',
		formatter : function Formatter(value, row, index) {
			if(row.emergencyContact==''){
				return '-'
			}else{
				return row.emergencyContact
			}
		} 
	}, {
		field : 'emergencyContactTel',
		align : 'center',
		formatter : function Formatter(value, row, index) {
			if(row.emergencyContactTel==''){
				return '-'
			}else{
				return row.emergencyContactTel
			}
		} 
	}, {
		align : 'type',
		width : 50,
		formatter : function Formatter(value, row, index) {
			if(row.type==0){
				return '无效'
			}else{
				return '有效'
			}
		} 
	}, {
		align : 'center',
		width : 200,
		formatter : actionFormatter
	}];
	$('#table').bootstrapTable(
			{
				url : "${pageContext.request.contextPath}/selectOwnerList",
				idField : "sid",
				columns : table_dataset,
				striped : true, //是否显示行间隔色
				pagination : true, //是否显示分页
				pageSize : 10, //每页的记录行数
				pageList : [ 10, 25, 50, 100 ], //可供选择的每页的行数
				search : false, //是否显示搜索
				showRefresh : true,
				sidePagination : "server", //表示服务端请求  
				queryParamsType : "limit",
				queryParams : function queryParams(params) { //设置查询参数  
					var param = {
						offset : params.offset,
						limit : params.limit,
						name : $("#name").val(),
						numberPeriods : $("#numberPeriods").val(),
						building : $("#building").val(),
						unit : $("#unit").val(),
						doorCard : $("#doorCard").val()
					};
					return param;
				},
				toolbar : "<button class='btn btn-primary glyphicon glyphicon-plus' onclick='addOwner(\"" + '' + "\");'>入住</button>"
						+"<div class='file-container' style='display:inline-block;position:relative;overflow: hidden;vertical-align:middle;margin-left:15px;'>"
		        		+"<button class='btn btn-primary fileinput-button glyphicon glyphicon-plus' type='button'>导入</button>"
		        		+"<input type='file' id='jobData' name='file' onchange='loadFile()' style='position:absolute;top:0;left:0;font-size:34px; opacity:0'></div>"
				  		+ "<div class='input-group' style='width:150px;float:right;margin-top:2px;margin-left:20px;'><input type='text' class='form-control' placeholder='请输入门牌号' id='doorCard'>"
						+ "<span class='input-group-btn' style='margin-left:20px;'><button class='btn btn-default' type='button' onclick='querry(\"" + '' + "\");'>查询</button></span></div>"
						+ "<div class='input-group' style='width:100px;float:right;margin-top:2px;margin-left:20px;'><input type='text' class='form-control' placeholder='请输入单元' id='unit'>"
						+ "</div>"
						+ "<div class='input-group' style='width:100px;float:right;margin-top:2px;margin-left:20px;'><input type='text' class='form-control' placeholder='请输入楼栋' id='building'>"
						+ "</div>"
						+ "<div class='input-group' style='width:100px;float:right;margin-top:2px;margin-left:20px;'><input type='text' class='form-control' placeholder='请输入期数' id='numberPeriods'>"
						+ "</div>"
						+ "<div class='input-group' style='width:100px;float:right;margin-top:2px;margin-left:15px;'><input type='text' class='form-control' placeholder='请输入姓名' id='name'>"
				  		+ "<span class='input-group-btn' style='margin-left:20px;'></span></div>",
				icons : {
					paginationSwitchDown : 'glyphicon-collapse-down icon-chevron-down',
					paginationSwitchUp : 'glyphicon-collapse-up icon-chevron-up',
					refresh : 'glyphicon-refresh icon-refresh',
					toggle : 'glyphicon-list-alt icon-list-alt',
					columns : 'glyphicon-th icon-th',
					detailOpen : 'glyphicon-plus icon-plus',
					detailClose : 'glyphicon-minus icon-minus',
					"export" : 'glyphicon-export icon-share'
				},
				showExport : true,
				exportDataType : 'basic',
				exportTypes : [ 'json', 'xml', 'csv', 'txt', 'sql', 'excel' ],
			});
	
	function loadFile(){
		var fileName=$("#jobData").val();
		if(fileName == '') {
	          layer.msg('请选择文件！',{
	        	  icon : 2,
				  time : 800
	          }); 
	          return false;
	      }
		var fileType = (fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length)).toLowerCase();
		if (!fileType == 'xlsx'|| !fileType == 'xls') {
			layer.msg('文件格式不正确！',{
	        	  icon : 2,
				  time : 800
	          }); 
			return false;
		} 
		$.ajaxFileUpload({
			fileElementId : 'jobData', //需要上传的文件域的ID，即<input type="file">的ID。
			url : "${pageContext.request.contextPath}/insertOwnerList", //后台方法的路径
			type : 'post', //当要提交自定义参数时，这个参数要设置成post
			dataType : 'json', //服务器返回的数据类型。可以为xml,script,json,html。如果不填写，jQuery会自动判断。
			secureuri : false, //是否启用安全提交，默认为false。
			async : true, //是否是异步
			success: function(data){
				if(data.result == 1){
					layer.msg("文件导入成功！", {
						icon : 1,
						time : 800
						});
					setTimeout(function() {
						$("#table").bootstrapTable('refresh');
					}, 500);
				}else{
					layer.msg("文件导入失败！", {
						icon : 2,
						time : 800
						});
				}
			},
			error : function(data){
				layer.msg('系统繁忙，请稍后再试!', {
					icon : 5,
					time : 800
				});
			}
		}); 
	}
	
	//弹窗
	function addOwner(id) {
		layer.open({
			type: 2,
			area: ['500px', '800px'],
			fix: false, //不固定
			maxmin: true,
			shade:0.4,
			offset: [50],
			title: '添加',
			content: '${pageContext.request.contextPath}/jump_addOwner?id=' + id,
			btn : [ "保存" ],
			btn1 : function(index, layero) {
				var iframeWin = window[layero.find('iframe')[0]['name']];
				iframeWin.insert();//执行子页面的方法
			}
		});

	}
	
	function updateHouse(id) {
		layer.open({
			type: 2,
			area: ['850px', '700px'],
			fix: false, //不固定
			maxmin: true,
			shade:0.4,
			offset: [50],
			title: '编辑',
			content: '${pageContext.request.contextPath}/jump_editHouse?id=' + id,
			btn : [ "保存" ],
			btn1 : function(index, layero) {
				var iframeWin = window[layero.find('iframe')[0]['name']];
				iframeWin.insert();//执行子页面的方法
			}
		});

	}
	

	function delOwner(id) {
			$.ajax({
				url : "${pageContext.request.contextPath}/deleteOwner",
				type : "post",
				data : {
					id : id,
					type:'0'
				},
				dataType : "json",
				success : function(data) {
					if (data.result === 1) {
						layer.msg('操作成功!', {
							icon : 1,
							time : 800
						});
						setTimeout(function() {
							$("#table").bootstrapTable('refresh');
						}, 500);
					} else {
						layer.msg('操作失败!', {
							icon : 2,
							time : 800
						});
					}
				},
				error : function() {
					layer.msg('系统繁忙，请稍后再试!', {
						icon : 5,
						time : 800
					});
				}
			});
	}

	function querry() {
		$("#table").bootstrapTable('refresh');
		$("#name").val(''),
		$("#numberPeriods").val(''),
		$("#building").val(''),
		$("#unit").val(''),
		$("#doorCard").val('')
	}
</script>
</html>
